

<?php

class ToolController extends Zend_Controller_Action {

    static $userName = '';
    static $userid = '';
    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'admin') {
                $this->_redirect('admin');
            }
        } else {
           self::$userName  = $auth->getIdentity()->username;
        }  
    }
    
    public function indexAction() {
        $this->_redirect('tool/girl');
    }

    public function girlAction() {
        $error = $this->_request->getParam('error');
        $this->view->username = self::$userName;
        $this->view->error = $error;
    }

    public function girlupdateAction() {
        $update = $this->_request->getParam("update");
        $girl = new Model_Girl();
        if (!empty($update)) {
            $title = $this->_request->getParam('title');
            $src = $this->_request->getParam('src');
            $id = $this->_request->getParam('id'); 
            if (!empty($title) && !empty($src) && !empty($id)) {
                $imageList = $this->_request->getParam('imagelist');
                $more = 0;
                if (!empty($imageList))
                    $more = 1;

                $data = array(
                    'id' => $id,
                    'title' => $title,
                    'img' => 'replace by id',
                    'src' => $src,
                    'more' => $more,
                    'admin' => self::$userName,
                );

                $girl = new Model_Girl();
                $girl->update_girl_album($id, $data);
                if ($more == 1) {
                    $img = explode("\n", $imageList);
                    if (!empty($img)) {
                        $girl->delete_girl_detail_albumid($id);
                        foreach ($img as $value) {
                            if (!empty($value)) {
                                $image_insert = array(
                                    'album_id' => $id,
                                    'src' => trim($value)
                                );
                                $girl->update_girl_detail_album($id, $image_insert);
                            }
                        }
                    }
                }
                $this->_redirect('tool/viewgirl?error=1', array());
            } else {
                $this->_redirect('tool/viewgirl?error=-110', array());
            }
        } else {
            $id = $this->_request->getParam("id") == "" ? 1 : $this->_request->getParam("id");            
            $data = $girl->get_album_for_update_by_album_id($id);
            $this->view->username = self::$userName;    
            $this->view->data = $data;
        }
    }

    public function girldeleteAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $id = $this->_request->getParam("id") == "" ? 1 : $this->_request->getParam("id");
        $girl = new Model_Girl();

        $girl->delete_girl_album($id);

        $this->_redirect('tool/viewgirl?error=1', array());
    }

    public function viewgirlAction() {
        $curren_page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
        $error = $this->_request->getParam('error');
        $count = 50;

        $girl = new Model_Girl();
        $data = $girl->get_album_by_page_toarray($curren_page, $count);

        //paging
        $total = $girl->get_album_total();

        $page = $total / $count + ($total % $count > 0 ? 1 : 0);
        
        $this->view->username = self::$userName;
        $this->view->error = $error;
        $this->view->girl = $data;
        $this->view->page = $page;
        $this->view->curren_page = $curren_page;
    }

    public function insertgirlAction() {
        $this->_helper->viewRenderer->setNoRender(true);

        $title = $this->_request->getParam('title');
        $src = $this->_request->getParam('src');

        if (!empty($title) && !empty($src)) {
            $imageList = $this->_request->getParam('imagelist');
            $more = 0;
            if (!empty($imageList))
                $more = 1;
            
            $data = array(
                'title' => $title,
                'img' => 'replace by id',
                'src' => $src,
                'more' => $more,
                'admin' => self::$userName,
            );
            
            $girl = new Model_Girl();
            $albumid = $girl->insert_girl_album($data);
            if ($albumid != -1 && $more == 1) {
                $img = explode("\n", $imageList);                
                if (!empty($img)) {
                    foreach ($img as $value) {
                        if (!empty($value)) {
                            $image_insert = array(
                                'album_id' => $albumid,
                                'src' => trim($value)
                            );
                            $girl->insert_girl_detail_album($image_insert);
                        }
                    }
                }
            }
            $this->_redirect('tool/girl?error=1', array());
        } else {
            $this->_redirect('tool/girl?error=-110', array());
        }
    }

    //video
    public function videoAction() {
        $error = $this->_request->getParam('error');
        $this->view->username = self::$userName;
        $this->view->error = $error;
    }

    public function insertvideoAction() {
        $this->_helper->viewRenderer->setNoRender(true);

        $title = $this->_request->getParam('title');
        $link = $this->_request->getParam('link');
        $thumb = $this->_request->getParam('thumb');
        $video_type = $this->_request->getParam('video_type');
        if (!empty($title) && !empty($link)) {
            if (empty($thumb) && strpos($link, 'youtube') !== false) {
                $id_utube = explode("?v=", $link);
                $thumb = 'http://i1.ytimg.com/vi/'.$id_utube[1].'/mqdefault.jpg';
            }
            $data = array(                
                'title' => $title,
                'thumb' => $thumb,
                'link' => $link,  
                'video_type' => $video_type,
                'admin' => self::$userName,
            );
            
            $girl = new Model_Video();
            $girl->insert_girl_video($data);
            $this->_redirect('tool/video?error=1', array());
        } else {
            $this->_redirect('tool/video?error=-110', array());
        }
    }
    
    public function viewvideoAction() {
        $curren_page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
        $error = $this->_request->getParam('error');
        $count = 50;

        $girl = new Model_Video();
        $data = $girl->get_girl_video_by_page($curren_page, $count);

        //paging
        $total = $girl->get_girl_video_total();

        $page = $total / $count + ($total % $count > 0 ? 1 : 0);
        
        $this->view->username = self::$userName;
        $this->view->error = $error;
        $this->view->video = $data;
        $this->view->page = $page;
        $this->view->curren_page = $curren_page;
    }
    
     public function videoupdateAction() {
        $update = $this->_request->getParam("update");
        $girl = new Model_Video();
        if (!empty($update)) {
            $title = $this->_request->getParam('title');
            $link = $this->_request->getParam('link');
            $id = $this->_request->getParam('id'); 
            $thumb = $this->_request->getParam('thumb');
            $video_type = $this->_request->getParam('video_type');            
            if (!empty($title) && !empty($link) && !empty($id)) {
                if (empty($thumb) && strpos($link, 'youtube') !== false) {
                    $id_utube = explode("?v=", $link);
                    $thumb = 'http://i1.ytimg.com/vi/'.$id_utube[1].'/mqdefault.jpg';
                }
                $data = array(
                    'id' => $id,
                    'title' => $title,
                    'thumb' => $thumb,
                    'video_type' => $video_type,
                    'link' => $link, 
                    'admin' => self::$userName,
                );
                $girl = new Model_Video();
                $girl->update_girl_video($id, $data);
                $this->_redirect('tool/viewvideo?error=1', array());
            } else {
                $this->_redirect('tool/viewvideo?error=-110', array());
            }
        } else {
            $id = $this->_request->getParam("id") == "" ? 1 : $this->_request->getParam("id");            
            $data = $girl->get_girl_video_for_update_by_id($id);
            $this->view->username = self::$userName;    
            $this->view->data = $data[0];
        }
    }

    public function videodeleteAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $id = $this->_request->getParam("id") == "" ? 1 : $this->_request->getParam("id");
        $girl = new Model_Video();
        $girl->delete_girl_video($id);
        $this->_redirect('tool/viewvideo?error=1', array());
    }
    
    public function userAction(){
        $curren_page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
        $error = $this->_request->getParam('error');
        $count = 50;

        $girl = new Model_Userreg();
        $data = $girl->get_user_by_page($curren_page, $count);

        //paging
        $total = $girl->get_user_total();

        $page = $total / $count + ($total % $count > 0 ? 1 : 0);
        
        $this->view->username = self::$userName;
        $this->view->error = $error;
        $this->view->user = $data;
        $this->view->page = $page;
        $this->view->curren_page = $curren_page;
    }
}

?>

